package ru.javadevelop.vegetarian.common;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Application;
import android.content.Context;
import android.util.Patterns;
import ru.javadevelop.vegetarian.model.*;

import java.util.ArrayList;
import java.util.regex.Pattern;

/**
 * http://t-menu.ru
 * User: navff
 * Date: 18.07.12
 * Time: 5:53
 */
public class ExtApplication extends Application {
    private static ExtApplication instatce;
    //******************************************************************************************************************
    public static ExtApplication getInstatce()
    {
        return instatce;
    }
    //******************************************************************************************************************
    public static class Session {
        public static String searchString;
        public static Class backActivity;
        public static Category category;
        public static Recipe recipe;
        public static String googleAccount;
        public static boolean showFavorites;

        //**************************************************************************************************************
        public static void clear() {
            ExtApplication.Session.searchString = null;
            ExtApplication.Session.backActivity = null;
        }
    }
    //******************************************************************************************************************
    @Override
    public final void onCreate()
    {
        super.onCreate();
        instatce = this;

        String dbUrl = "jdbc:h2:mem:";
        DB.INSTANCE.openConnection(dbUrl, "sa", "sa");
        getServerVariables();
        setMainGoogleAccount();

    }
    //******************************************************************************************************************
    @Override
    public void onTerminate() {
        super.onTerminate();
        DB.INSTANCE.closeConnection();
    }
    //******************************************************************************************************************
    public void getServerVariables() {
        try
        {
            AppPreferences.setDefaultParams();
            FileUtils.setSoragePlace(this);

        } catch (Exception ex)
        {
            throw new RuntimeException("Не могу получить данные");
        }
    }
    //******************************************************************************************************************
    public void setMainGoogleAccount()
    {
        Context context = getApplicationContext();
        Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
        Account[] accounts = AccountManager.get(context).getAccounts();
        for (Account account : accounts) {
            if (emailPattern.matcher(account.name).matches()) {
                Session.googleAccount = account.name;
                return;
            }
        }
    }
    //******************************************************************************************************************
    //******************************************************************************************************************
    //******************************************************************************************************************
    //******************************************************************************************************************
}
